<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页面</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/详情页面专用CSS.css">

    <link rel="stylesheet" href="editor.md-master/css/editormd.min.css">
    <!-- <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> -->
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="editor.md-master/lib/marked.min.js"></script>
    <script src="editor.md-master/lib/prettify.min.js"></script>
    <script src="editor.md-master/editormd.js"></script>
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="image/狗狗.png" alt="">
        <sapn class="title">我的博客系统</sapn>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧区域，显示用户信息 -->
        <div class="container-left">
            <!-- 用户详情 -->
            <div class="card">
                <!-- 头像 -->
                <img src="image/头像.png" alt="">
                <!-- 用户名字 -->
                <h3></h3>
                <!-- gitee地址链接 -->
                <a href="https://gitee.com/Coach_zhang">博主的gitee链接</a>
                <!-- 显示文章分类的数目 -->
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧区域，博客详情 -->
        <div class="container-right">
            <div class="blog-content">
                <h3></h3>
                <div class="date"></div>
                <div id="content" style="background-color: transparent;">

                </div>
            </div>
        </div>
    </div>
    <script>
        $.ajax({
            url: "blog" + location.search,
            method:"get",

            success:function(data,status){
                buildBlog(data);
            },

            error:function(){
                location.assign('blog_login.html');
            }

        });

        function buildBlog(data){

            let titleH3=document.querySelector('.blog-content h3');
            titleH3.innerHTML=data.title;

            let dateDiv=document.querySelector('.blog-content .date');
            dateDiv.innerHTML=formatDate(data.postTime);

            editormd.markdownToHTML('content',{markdown: data.content});
        }

        function formatDate(time){
            var date = new Date(time);

	        var year = date.getFullYear(),
		        month = date.getMonth()+1,
		        day = date.getDate(),
		        hour = date.getHours(),
		        min = date.getMinutes(),
		        sec = date.getSeconds();
	        var newTime = year + '-' +
				(month < 10? '0' + month : month) + '-' +
				(day < 10? '0' + day : day) + ' ' +
				(hour < 10? '0' + hour : hour) + ':' +
				(min < 10? '0' + min : min) + ':' +
				(sec < 10? '0' + sec : sec);
	        return newTime;	
        }

        $.ajax({
            url:'user'+location.search,
            method:'get',
            success:function(data,status){
                changeUser(data);
            }
        });

        function changeUser(user){
            let h3=document.querySelector('.container-left .card h3');
            h3.innerHTML=user.username;

            if(user.isYourBlog){
                let deleteButton=document.createElement('a');
                deleteButton.innerHTML='删除';
                deleteButton.href='blogDelete'+location.search;
                let navDiv=document.querySelector('.nav');
                navDiv.appendChild(deleteButton);
            }
        }
    </script>
</body>
</html>